package com.grant.code.utils;

import java.io.File;
import java.util.Set;

import static com.grant.code.controller.VideoController.ALLOWED_EXTENSIONS;


public class FileUtils {
    private static final Set<String> IMAGE_EXTENSIONS = Set.of(
        "jpg", "jpeg", "png", "gif", "bmp"
    );

    public boolean isImageFile(File file) {
        String name = file.getName().toLowerCase();
        return name.matches(".*\\.(jpg|jpeg|png|gif)$");
    }

    public static String formatFileSize(long bytes) {
        if (bytes < 1024) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        String unit = "KMGTPE".charAt(exp-1) + "B";
        return String.format("%.1f %s", bytes / Math.pow(1024, exp), unit);
    }

    public static String getFileExtension(File file) {
        String name = file.getName();
        int lastIndexOf = name.lastIndexOf(".");
        if (lastIndexOf == -1) return "";
        return name.substring(lastIndexOf + 1).toLowerCase();
    }

    // 重命名并修改验证方法
    private boolean isValidMediaFile(File file) {
        String fileName = file.getName().toLowerCase();
        for (String ext : ALLOWED_EXTENSIONS) {
            if (fileName.endsWith(ext)) return true;
        }
        return false;
    }

    // 在 processVideoFile 中替换验证调用
    private void processVideoFile(File file) {

        if (!isValidMediaFile(file)) {
            DialogUtils.showErrorDialog("格式错误",
                    "支持格式: MP4/AVI/MOV/JPG/JPEG/PNG/GIF");
            return;
        }
    }

}